import Base from 'models/base';

export default class BindPhoneData extends Base {
  static labelTagList = ['学生党', '宝妈宝爸', '爱车一族', '上班族', '园丁教师', '美食达人', '旅游达人', '手艺达人'];
  static phoneReg = /^1[3456789]\d{9}$/;
  userName = '';
  phoneNum = '';
  phoneCode = '';
  sendPhone = '';
  tagList = [];

  constructor(user) {
    super();
    this.userName = user.nickname;

    let labelTagList = BindPhoneData.labelTagList;
    let tagList = [];
    for (let i = 0; i < labelTagList.length; i++) {
      const tag = labelTagList[i];
      tagList.push({
        value: tag,
        label: tag,
        chekced: false
      });
    }
    this.tagList = tagList;
  }

  checkValid(hasSend = false) {
    let fields = ['phoneNum'];
    if (!hasSend) {
      fields.push('phoneCode');
    }
    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let phoneReg = BindPhoneData.phoneReg;
    if (!phoneReg.test(this.phoneNum)) {
      return 'phoneNumInvalid';
    }
    return '';
  }

  getObject() {
    let tagList = this.tagList;
    let tags = [];
    for (let i = 0; i < tagList.length; i++) {
      const tag = tagList[i];
      if (tag.chekced) {
        tags.push(tag.value);
      }
    }
    let postData = {
      phoneNum: this.phoneNum,
      phoneCode: this.phoneCode,
      weChatUserInfoDetailTag: JSON.stringify(tags)
    };

    return postData;
  }
}
